package com.lancoo.cvas.exception;


import cn.hutool.core.util.ObjUtil;
import com.lancoo.cvas.util.EnumResultCode;
import com.lancoo.cvas.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author quin
 * @ClassName GlobalExceptionHandler.java
 * @Description 全局异常处理
 * @createTime 2022年05月19日 14:10:00
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {


    /**
     * 处理自定义的业务异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = BizException.class)
    public Result bizExceptionHandler(HttpServletRequest req, BizException e) {
        log.error("【internal server error,requestURI:{},e is :{}  {}】", req.getRequestURI(), e,getStackTrace(e));
        if (ObjUtil.isNull(e.getErrorCode())) {
            e.setErrorCode(EnumResultCode.FAIL.getCode());
        }
        e.printStackTrace();
        return Result.ofFail(e.getErrorCode(), e.getErrorMsg());
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public Result validHandler(HttpServletRequest req, MethodArgumentNotValidException e) {
        log.error("【internal server error,requestURI:{},e is :{}  {}】", req.getRequestURI(), e,getStackTrace(e));
        e.printStackTrace();
        return Result.ofFail(EnumResultCode.PARAMS_ERROR, e.getFieldError().getDefaultMessage());
    }

    /**
     * 处理空指针的异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = NullPointerException.class)
    public Result exceptionHandler(HttpServletRequest req, NullPointerException e) {
        log.error("【internal server error,requestURI:{},e is :{}  {}】", req.getRequestURI(), e,getStackTrace(e));
        e.printStackTrace();
        return Result.ofFail(2, "NullPointerException", e.getMessage());
    }

    /**
     * 处理404的异常
     *
     * @return
     */
    @ExceptionHandler(value = NoHandlerFoundException.class)
    public Result noHandlerFoundException(HttpServletRequest req, Exception e) {
        log.error("【internal server error,requestURI:{},e is :{}  {}】", req.getRequestURI(), e,getStackTrace(e));
        e.printStackTrace();
        return Result.ofFail(2, "NoHandlerFoundException", e.getMessage());
    }

    /**
     * 处理其他异常，服务器内部错误
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public Result exceptionHandler(HttpServletRequest req, Exception e) {
        log.error("【internal server error,requestURI:{},e is :{}  {}】", req.getRequestURI(), e,getStackTrace(e));
        e.printStackTrace();
        return Result.ofFail(EnumResultCode.FAIL, e.getMessage());
    }

    @ExceptionHandler(value = CapacityException.class)
    public Result capacityExceptionHandler(HttpServletRequest req, CapacityException e) {
        return Result.ofFail(2, e.getMessage());
    }

    /**
     * 获取堆栈信息
     * @param throwable
     * @return
     */
    public static String getStackTrace(Throwable throwable){
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try
        {
            throwable.printStackTrace(pw);
            return sw.toString();
        } finally
        {
            pw.close();
        }
    }

}
